package com.hzw.saas.service.property.controller;

import javax.annotation.Resource;

import com.hzw.saas.api.property.IPropertyService;
import com.hzw.saas.api.property.enmus.SysDictKeyEnum;
import com.hzw.saas.api.property.enmus.SysParamKeyEnum;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.RedisUtil;
import com.hzw.saas.common.util.constants.CacheConsts;
import com.hzw.saas.service.property.param.PropertyParam;
import com.hzw.saas.service.property.service.SysDictServiceImpl;
import com.hzw.saas.service.property.service.SysParamServiceImpl;
import cn.hutool.core.util.StrUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;

/**
 * @author zzl
 * @since 05/06/2021
 */
@Controller
@RequiredArgsConstructor
@Api(tags = "应用参数")
public class ParamsController {

    @Resource(name = IPropertyService.SYS_PARAM_BEAN)
    private IPropertyService<SysParamKeyEnum> sysParamService;

    @Resource(name = IPropertyService.SYS_DICT_BEAN)
    private IPropertyService<SysDictKeyEnum> sysDictService;

    @GetMapping("/app/host/web")
    @ApiOperation(value = "获取应用前端Host", notes = "获取应用前端Host")
    @ApiResponses({
        @ApiResponse(code = 200, message = "前端Host, e.g. 68.79.28.186:8081")
    })
    public ResponseEntity<String> getWebHost() {
        String host = sysParamService.getPropertyValue(SysParamKeyEnum.APP_FRONTEND_HOST, "");
        return ResponseEntity.ok(host);
    }

    @GetMapping("/app/property/module/{module}/key/{key}")
    @ApiOperation(value = "获取应用字典值", notes = "获取应用字典值")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "module", value = "模块名", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "String", paramType = "path"),
    })
    @ApiResponses({
        @ApiResponse(code = 200, message = "key对应的value")
    })
    public ResponseEntity<String> getAppProperty(@PathVariable String module, @PathVariable String key) {
        String value = sysDictService.getPropertyValue(module, key, "");
        return ResponseEntity.ok(value);
    }

    @GetMapping("/app/param/module/{module}/key/{key}")
    @ApiOperation(value = "获取系统参数值", notes = "获取系统参数值")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "module", value = "模块名", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "String", paramType = "path"),
    })
    @ApiResponses({
        @ApiResponse(code = 200, message = "key对应的value")
    })
    public ResponseEntity<String> getAppParam(@PathVariable String module, @PathVariable String key) {
        String value = sysParamService.getPropertyValue(module, key, "");
        return ResponseEntity.ok(value);
    }

    @DeleteMapping("/sys/param/cache")
    @SysLog(operation = "清理系统参数缓存", mode = 3)
    @ApiOperation("清理系统参数缓存")
    public ResponseEntity<Void> clearParamCache(@RequestBody PropertyParam param) {
        String key = SysParamServiceImpl.CACHE_GROUP;
        this.delCache(key, param);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/sys/dic/cache")
    @SysLog(operation = "清理字典参数缓存", mode = 3)
    @ApiOperation("清理字典参数缓存")
    public ResponseEntity<Void> clearDictCache(@RequestBody PropertyParam param) {
        String key = SysDictServiceImpl.CACHE_GROUP;
        this.delCache(key, param);
        return ResponseEntity.ok().build();
    }

    private void delCache(String key, PropertyParam param) {
        StringBuilder kb = new StringBuilder(key);
        kb.append(CacheConsts.CACHE_KEY_JOINER).append(CacheConsts.CACHE_KEY_JOINER);
        if (StrUtil.isBlank(param.getModule())) {
            kb.append(CacheConsts.CACHE_KEY_WILDCARD);
            RedisUtil.delByPattern(kb.toString());
            return;
        }
        kb.append(param.getModule()).append(CacheConsts.CACHE_KEY_JOINER);
        if (StrUtil.isBlank(param.getKey())) {
            kb.append(CacheConsts.CACHE_KEY_WILDCARD);
            RedisUtil.delByPattern(kb.toString());
            return;
        }
        kb.append(param.getKey());
        RedisUtil.del(kb.toString());
    }

}
